{% extends "base.html" %}
{% block content %}
<h1>Use case from discussions</h1>
<h2>Comments</h2>
<p>
  Initial need is to factorize form process on submitting new
  comment. The trick is to redirect to object commented, and the first
  thought solution was to insert logic in templates... <a
  href="http://groups.google.com/group/django-users/browse_thread/thread/034db84e651112a2/02b75f3fc4cf9375?hl=fr"
  target="_blank">discussion thread here</a>  
</p>
<p>
  In this case, form views are perfect for the job and quite
  effective. Just use <code>process_form</code> to built up comment
  and <code>on_success</code> to redirect to commented object...</p>
<!-- http://dpaste.com/129828/ -->
<div class="highlight">
<pre><span class="c"># import obviews lib (http://code.google.com/p/django-generate-views/)</span>
<span class="k">from</span> <span class="nn">obviews.views</span> <span class="k">import</span> <span class="n">FormView</span>

<span class="k">class</span> <span class="nc">CommentFormView</span><span class="p">(</span><span class="n">FormView</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;comments/add.html&quot;</span>
    <span class="n">form_class</span> <span class="o">=</span> <span class="n">CommentForm</span>

    <span class="c"># process the data of your form here</span>
    <span class="k">def</span> <span class="nf">process_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">variables</span><span class="p">):</span>
        <span class="c"># get commented object (could be done from URL with build_params)</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">ContentType</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get_for_model</span><span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&quot;model&quot;</span><span class="p">])</span>
        <span class="n">obj</span> <span class="o">=</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&quot;eid&quot;</span><span class="p">])</span>
        <span class="c"># create comment</span>
        <span class="n">comment</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">commit</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
        <span class="n">comment</span><span class="o">.</span><span class="n">poster</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span>
        <span class="n">comment</span><span class="o">.</span><span class="n">content_type</span> <span class="o">=</span> <span class="n">model</span>
        <span class="n">comment</span><span class="o">.</span><span class="n">object_id</span> <span class="o">=</span> <span class="n">obj</span><span class="o">.</span><span class="n">pk</span>
        <span class="k">return</span> <span class="n">comment</span>

    <span class="c"># overwrite on_success and use result of process_form</span>
    <span class="k">def</span> <span class="nf">on_success</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">variables</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">object</span><span class="o">.</span><span class="n">get_absolute_url</span><span class="p">())</span>
</pre></div>
{% endblock %}
